package com.example.famhealing.schedule;

import java.util.Calendar;

import android.content.Context;
import android.graphics.Color;
import android.text.format.Time;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;

//달력 어댑터
public class CalendarMonthAdapter extends BaseAdapter {

	Context mContext;
	
	private int selectedPosition = -1;
	
	private MonthItem[] items;
	
	//열 갯수 설정
	private int countColumn = 7;
	
	int mStartDay;
	int startDay;
	int curYear;
	int curMonth;
	int curDay;
	
	int firstDay;
	int lastDay;
	
	int prevLastDay;
	
	//날짜 여러개 선택 가능/불가능 변경 위한 변수
	public static boolean multiSelect = false;
	
	Calendar mCalendar;
	boolean recreateItems = false;
	
	public CalendarMonthAdapter(Context context) {
		super();

		mContext = context;
		
		init();
	}
	
	public CalendarMonthAdapter(Context context, AttributeSet attrs) {
		super();

		mContext = context;
		
		init();
	}

	private void init() {
		items = new MonthItem[7 * 6];

		mCalendar = Calendar.getInstance();
		recalculate();
		resetDayNumbers();
		
	}
	
	public void recalculate() {

		mCalendar.set(Calendar.DAY_OF_MONTH, 1);
		
		int dayOfWeek = mCalendar.get(Calendar.DAY_OF_WEEK);
		firstDay = getFirstDay(dayOfWeek);
		
		mStartDay = mCalendar.getFirstDayOfWeek();
		curYear = mCalendar.get(Calendar.YEAR);
		curMonth = mCalendar.get(Calendar.MONTH);
		//curDay = mCalendar.get(Calendar.DATE);
		lastDay = getMonthLastDay(curYear, curMonth);
		
		//전월 마지막날짜 계산
		if(curMonth == 0)
			prevLastDay = getMonthLastDay(curYear-1, 11);
		else
			prevLastDay = getMonthLastDay(curYear, curMonth-1);
		
		int diff = mStartDay - Calendar.SUNDAY - 1;
        startDay = getFirstDayOfWeek();
		
	}
	
	//이전월로 넘어감
	public void setPreviousMonth() {
		mCalendar.add(Calendar.MONTH, -1);
        recalculate();
        
        resetDayNumbers();
        selectedPosition = -1;
	}
	
	//다음월로 넘어감
	public void setNextMonth() {
		mCalendar.add(Calendar.MONTH, 1);
        recalculate();
        
        resetDayNumbers();
        selectedPosition = -1;
	}
	
	//날짜 reset
	public void resetDayNumbers() {
		
		for (int i = 0; i < 42; i++) {

			//날짜 계산
			int dayNumber = (i+1) - firstDay;
			
			if(dayNumber < 1) {
				dayNumber = prevLastDay + dayNumber;
			} else if(dayNumber > lastDay) {
				dayNumber = dayNumber-lastDay;
			}
			
	        items[i] = new MonthItem(dayNumber, false);
		}
	}
	
	
	private int getFirstDay(int dayOfWeek) {
		int result = 0;
		if (dayOfWeek == Calendar.SUNDAY) {
			result = 0;
		} else if (dayOfWeek == Calendar.MONDAY) {
			result = 1;
		} else if (dayOfWeek == Calendar.TUESDAY) {
			result = 2;
		} else if (dayOfWeek == Calendar.WEDNESDAY) {
			result = 3;
		} else if (dayOfWeek == Calendar.THURSDAY) {
			result = 4;
		} else if (dayOfWeek == Calendar.FRIDAY) {
			result = 5;
		} else if (dayOfWeek == Calendar.SATURDAY) {
			result = 6;
		}
		
		return result;
	}
	
	
	public int getCurYear() {
		return curYear;
	}
	
	public int getCurMonth() {
		return curMonth;
	}
	
	public int getNumColumns() {
		return 7;
	}

	public int getCount() {
		return 7 * 6;
	}

	public Object getItem(int position) {
		return items[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		MonthItemView itemView;
		
		if (convertView == null) {
			itemView = new MonthItemView(mContext);
		} else {
			itemView = (MonthItemView) convertView;
		}	
		
		GridView.LayoutParams params = new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, 120);
		
		//row, column계산
		int rowIndex = position / countColumn;
		int columnIndex = position % countColumn;

		//set properties
		itemView.setItem(items[position]);
		itemView.setLayoutParams(params);
		itemView.setPadding(2, 2, 2, 2);
		//itemView.setGravity(Gravity.LEFT);
		
		//평일, 토, 일 색깔 지정
		if (columnIndex == 0) {
			itemView.setTextColor(Color.RED);
		} else if(columnIndex == 6){
			itemView.setTextColor(Color.BLUE);
		} else {
			itemView.setTextColor(Color.BLACK);
		}
		
		//날짜 선택시
		if(multiSelect) {
			if (position == getSelectedPosition()) {
				itemView.setBackgroundColor(Color.CYAN);
			}
		} else {
			if (position == getSelectedPosition()) {
				itemView.setBackgroundColor(Color.CYAN);
			} else {
				itemView.setBackgroundColor(Color.WHITE);
			}
		}
        

		return itemView;
	}

    public static int getFirstDayOfWeek() {
        int startDay = Calendar.getInstance().getFirstDayOfWeek();
        if (startDay == Calendar.SATURDAY) {
            return Time.SATURDAY;
        } else if (startDay == Calendar.MONDAY) {
            return Time.MONDAY;
        } else {
            return Time.SUNDAY;
        }
    }
 
    private int getMonthLastDay(int year, int month){
    	//각 월의 날짜수 계산(case 뒤의 숫자+1이 실제 월)
    	switch (month) {
 	   		case 0:
      		case 2:
      		case 4:
      		case 6:
      		case 7:
      		case 9:
      		case 11:
      			return (31);

      		case 3:
      		case 5:
      		case 8:
      		case 10:
      			return (30);

      		default:
      			//2월 날짜수 계산
      			if(((year%4==0)&&(year%100!=0)) || (year%400==0) ) {
      				return (29);
      			} else { 
      				return (28);
      			}
 	   	}
 	}
    
	public void setSelectedPosition(int selectedPosition) {
		this.selectedPosition = selectedPosition;
	}

	public int getSelectedPosition() {
		return selectedPosition;
	}
	
}
